package com.group02.community.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group02.community.annotation.Log;
import com.group02.community.enmus.BusinessType;
import com.group02.community.entity.log.SysLogininfor;
import com.group02.community.service.SysLogininforService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * 系统访问记录 前端控制器
 * </p>
 *
 * @author xl
 * @since 2022-07-01
 */
@Api
@CrossOrigin
@RestController
@RequestMapping("/intelligence_community/sys-logininfor")
public class SysLogininforController {

    @Autowired
    private SysLogininforService sysLogininforService;

    @ApiOperation("登录日志列表")
    @PostMapping("loginLogList")
    public ResponseEntity<IPage<SysLogininfor>> LoginLogPage(Long current, Long size, @RequestBody SysLogininfor sysLogininfor, String dateRange){
        IPage<SysLogininfor> loginLogIPage = sysLogininforService.loginLogPage(current,size,sysLogininfor,dateRange);
        return ResponseEntity.ok(loginLogIPage);
    }

    @ApiOperation("登录日志添加或修改")
    @PostMapping("loginLogSaveOrUpdate")
    public ResponseEntity<String> LoginLogSaveOrUpdate(SysLogininfor sysLoginLog){
        sysLogininforService.save(sysLoginLog);
        return ResponseEntity.ok("添加或修改成功");
    }

    @ApiOperation("登录日志删除")
    @DeleteMapping("loginLogDelete/{id}")
    @Log(title = "登录日志",businessType = BusinessType.DELETE)
    public ResponseEntity<String> LoginLogDelete(@PathVariable Long[] id){
        sysLogininforService.loginLogDelete(id);
        return ResponseEntity.ok("删除成功");
    }

    @GetMapping("downloadExcel")
    @ApiOperation("导出数据")
    @Log(title = "登录日志",businessType = BusinessType.EXPORT)
    public ResponseEntity<String> downloadExcel(HttpServletResponse response) throws IOException {
        sysLogininforService.downloadExcel(response);
        return ResponseEntity.ok("导出成功");
    }

    @GetMapping("emptyDatabase")
    @ApiOperation("清空数据库")
    @Log(title = "登录日志",businessType = BusinessType.CLEAN)
    public ResponseEntity<String> emptyDatabase(){
        sysLogininforService.emptyDatabase();
        return ResponseEntity.ok("清空数据库");
    }
}

